<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2017-11-17
 * Time: 15:51
 * @desc   商城后台 -- 库存管理
 */
namespace app\mall\controller;
use controller\BasicAdmin;
use service\DataService;
use think\Db;
use think\Request;
class Chengpin extends BasicAdmin{
	/**
	 * 总部报损管理 -- 列表
	 * @return [type] [description]
	 */
	public function index(){
		$this->title = '成品库存';
		$table = "Goods";
        $get = $this->request->get();
        $db = Db::name($table)->where(['is_deleted' => '0','is_zigou'=>1]);
        $warn = db($table)->where(['is_deleted' => '0','is_zigou'=>1])->select();
        $num = 0;
        foreach($warn as $k => $v){
        	if($v['stock'] < $v['warn_stock']) $num++;
        }
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }
        $this->assign('num',$num);
        return parent::_list($db);
	}
	/**
	 * 变更库存 -- 表单
	 * @return [type] [description]
	 */
	public function change_stock(){
		if(request()->isPost()){
			$data = input('post.');		//提交数据
			if(($data['origin_stock']<$data['dif_num']) && ($data['sign'] == 3)) $this->error('报损数不能大于现有库存数，请重新填写','');
			$data['sign'] == 1?$data['type']=1:$data['type']=2;
			if($data['sign'] == 1) $data['dif_num'] = $data['del_num']-$data['res_num'];
			$data['sign'] == 1?$data['cur_num']=$data['origin_stock']+$data['res_num']:$data['cur_num']=$data['origin_stock']-$data['dif_num'];
			if($data['sign'] == 2) $data['res_num']=$data['del_num'];
			Db::name('goods')->where('id',$data['id'])->update(['stock'=>$data['cur_num']]);		//更新商品库存
			$res = Db::name('material_stock_log')->insertGetId(['store_id'=>$data['store_id'],'material_id'=>$data['id'],'material_type'=>$data['material_type'],'sign'=>$data['sign'],'type'=>$data['type'],'del_num'=>$data['del_num'],'res_num'=>$data['res_num'],'dif_num'=>$data['dif_num'],'cur_num'=>$data['cur_num'],'remark'=>$data['remark'],'addtime'=>$data['addtime']]);		//过滤不存在的字段
			$res > 0?$this->success('保存成功',''):$this->error('保存失败','');
		}
		$this->assign('addtime',time());
        return $this->_form('goods', 'change_stock');
	}
	/**
	 * 设置安全库存 -- 表单
	 */
	public function set_warn(){
		return $this->_form('goods', 'warn_stock');
	}
	/**
	 * 达到安全预警的成品 -- 列表
	 * @return [type] [description]
	 */
	public function warn_list(){
		$this->title = '缺货商品';
		$table = "Goods";
        $get = $this->request->get();
        $db = Db::name($table)->where(['is_deleted' => '0','is_stock'=>1]);
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }
        return parent::_list($db);
	}
	/**
	 * 商品库存
	 * @return [type] [description]
	 */
	public function chengpin_stock_log(){
		$this->title = '成品库存日志';
		$table = "MaterialStockLog";			//商品库存表
        $get = $this->request->get();
        $map['a.material_type'] = array('eq',2);	//自购成品库存
        $join = [['ys_goods b','a.material_id=b.id','LEFT']];
        if(!empty($get['material_id'])){
        	$map['a.material_id'] = array('eq',$get['material_id']);
        }
        $db = Db::name($table)->alias('a')->join($join)->field('a.*,b.name,b.unit')->where($map);
        $goods = db('goods')->where(['is_deleted'=>0,'is_zigou'=>1])->field('id,name')->select();
        $this->assign('goods',$goods);
        if(!isset($get['export'])){
        	return parent::_list($db);
        }else{
        	$data = Db::name($table)->alias('a')->join($join)->field('a.*,b.name,b.unit')->where($map)->select();
        	$this->export($data);
        }
	}
	/**
	 * 导出成品库存日志记录 -- 导出
	 * @return [type] [description]
	 */
	private function export($list){
		$table = '';
		$table .= "<table>
						<thead>
						<tr>
							<th>".iconv('UTF-8', 'GB2312','名称')."</th>	
							<th>".iconv('UTF-8', 'GB2312','类型')."</th>
                    		<th>".iconv('UTF-8', 'GB2312','变更数')."</th>
                    		<th>".iconv('UTF-8', 'GB2312','当前库存')."</th>
                    		<th>".iconv('UTF-8', 'GB2312','变更时间')."</th>
                   	 	</tr>
                   	 	</thead>
                   	<tbody>";
        foreach ($list as $v){
            $table .= "<tr>
                    		<td>".iconv('UTF-8', 'GB2312',$v['name'])."</td>";
            if($v['sign'] == 1){
            	$table .= "<td>".iconv('UTF-8', 'GB2312','进货')."</td>";
            }else if($v['sign'] == 2){
            	$table .= "<td>".iconv('UTF-8', 'GB2312','提货')."</td>";
            }else{
            	$table .= "<td>".iconv('UTF-8', 'GB2312','报损')."</td>";
            }
            if($v['type'] == 1){
            	$table .= "<td>+".iconv('UTF-8', 'GB2312',$v['res_num'])."</td>";
            }else{
            	$table .= "<td>-".iconv('UTF-8', 'GB2312',$v['res_num'])."</td>";
            }
			$table .= "<td>".iconv('UTF-8', 'GB2312',$v['cur_num'])."</td>";
           	$time = date('Y-m-d H:i',$v['addtime']);
           	$table .="<td>".iconv('UTF-8', 'GB2312',$time)."</td>";
        }
        $table .= "</tr></tbody></table>";
        header("Pragma: public");  
        header("Expires: 0");  
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");  
        header("Content-Type:application/force-download");  
        header("Content-Type:application/vnd.ms-execl");  
        header("Content-Type:application/octet-stream");  
        header("Content-Type:application/download");;  
        header('Content-Disposition:attachment;filename="成品库存明细表.xls"');  
        header("Content-Transfer-Encoding:binary"); 
        echo $table;exit();			//必须加上exit();   防止导出调试信息
	}
}
?>